package com.ruoyi.outuser.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.outuser.mapper.OutUserMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.outuser.domain.OutUser;
import com.ruoyi.outuser.service.IOutUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户管理Controller
 * 
 * @author shz
 * @date 2024-07-20
 */
@RestController
@RequestMapping("/outuser/outuser")
public class OutUserController extends BaseController
{
    @Autowired
    private IOutUserService outUserService;
    @Resource
    OutUserMapper outUserMapper;
    /**
     * 查询用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('outuser:outuser:list')")
    @GetMapping("/list")
    public TableDataInfo list(OutUser outUser)
    {
        startPage();
        List<OutUser> list = outUserService.selectOutUserList(outUser);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('outuser:outuser:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OutUser outUser)
    {
        List<OutUser> list = outUserService.selectOutUserList(outUser);
        ExcelUtil<OutUser> util = new ExcelUtil<OutUser>(OutUser.class);
        util.exportExcel(response, list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('outuser:outuser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(outUserService.selectOutUserById(id));
    }

    /**
     * 新增用户管理
     */
    @PreAuthorize("@ss.hasPermi('outuser:outuser:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OutUser outUser)
    {
        if(outUserMapper.isUserNameOne(outUser.getUsername())>0){
            return  error("用户名已存在");
        }
        outUser.setStudyCourse(0L);
        outUser.setYearCourse(0L);

        return toAjax(outUserService.insertOutUser(outUser));
    }

    /**
     * 修改用户管理
     */
    @PreAuthorize("@ss.hasPermi('outuser:outuser:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OutUser outUser)
    {
        return toAjax(outUserService.updateOutUser(outUser));
    }

    /**
     * 删除用户管理
     */
    @PreAuthorize("@ss.hasPermi('outuser:outuser:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(outUserService.deleteOutUserByIds(ids));
    }
    /**
     * 登录
     */
    @Resource
    SysLoginService loginService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody){
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @PostMapping("/register")
    public AjaxResult add2(@RequestBody OutUser outUser)
    {
        if(outUserMapper.isUserNameOne(outUser.getUsername())>0){
            return  error("用户名已存在");
        }
        outUser.setStudyCourse(0L);
        outUser.setYearCourse(0L);

        return toAjax(outUserService.insertOutUser(outUser));
    }

}
